﻿using DOC2TXT.Forms;
using DOC2TXT.Library;
using HTMeeting.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace HTMeeting
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            string watchfolder = System.IO.Directory.GetCurrentDirectory() + "\\wordfiles";
            WatcherStrat(watchfolder, "*.doc*");

            var uc = new UCList();
            uc.Dock = DockStyle.Fill;
            uc.ShowData();

            this.panel1.Controls.Add(uc);

        }

        private void btnAddMeeting_Click(object sender, EventArgs e)
        {
            var uc = new UCList();
            uc.Dock = DockStyle.Fill;
            DisPanel();
            uc.ShowData();
            this.panel1.Controls.Add(uc); 

            
        }
        /// <summary>
        /// 监控文件夹
        /// </summary>
        /// <param name="path"></param>
        /// <param name="filter"></param>
        FileSystemWatcher watcher = null;
        public void WatcherStrat(string path, string filter)
        {

            watcher = new FileSystemWatcher();
            watcher.Path = path;
            watcher.Filter = filter;         

            watcher.Changed += new FileSystemEventHandler(OnProcess);
            watcher.Created += new FileSystemEventHandler(OnProcess);
            watcher.Deleted += new FileSystemEventHandler(OnProcess);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);
            watcher.EnableRaisingEvents = true;
            watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess
                                   | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
            watcher.IncludeSubdirectories = true;
        }
        private  void OnProcess(object source, FileSystemEventArgs e)
        {
            if (e.ChangeType == WatcherChangeTypes.Created)
            {
                OnCreated(source, e);
            }
            else if (e.ChangeType == WatcherChangeTypes.Changed)
            {
                OnChanged(source, e);
            }
            else if (e.ChangeType == WatcherChangeTypes.Deleted)
            {
                OnDeleted(source, e);
            }

        }
        private  void OnCreated(object source, FileSystemEventArgs e)
        {
            try
            {
                string searchstr = "select id from wordfiles where fullpath='" + e.FullPath + "'";
                var ds = SQLiteHelper.ExecuteDataSet(searchstr, CommandType.Text);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string updatesql = "update wordfiles set ";
                    updatesql += "fullpath='" + e.FullPath + "',";
                    var pname = e.Name.Substring(0, e.Name.IndexOf('.'));
                    updatesql += "pname='" + pname + "',";
                    updatesql += "uploadtime=datetime('now','localtime'),";
                    var html = Word2HtmlHandler.Word2Html(e.FullPath);
                    updatesql += "htmlcon='" + html + "',";
                    var text = Word2HtmlHandler.Word2Txt(e.FullPath);
                    updatesql += "txtcon='" + text + "' ";
                    updatesql += " where fullpath='" + e.FullPath + "'";
                    if (html != "" && text != "")
                    {
                        int i = SQLiteHelper.ExecuteNonQuery(updatesql, CommandType.Text);
                    }
                }
                else
                {
                    string sql = "insert into wordfiles(pname,fullpath,uploadtime,state,htmlcon,txtcon)values(";
                    var pname = e.Name.Substring(0, e.Name.IndexOf('.'));
                    sql += "'" + pname + "',";
                    sql += "'" + e.FullPath + "',";
                    sql += "datetime('now','localtime'),";
                    sql += "0,";
                    var html = Word2HtmlHandler.Word2Html(e.FullPath);
                    sql += "'" + html + "',";
                    var text = Word2HtmlHandler.Word2Txt(e.FullPath);
                    sql += "'" + text + "')";
                    if (html != "" && text != "")
                    {
                        int i = SQLiteHelper.ExecuteNonQuery(sql, CommandType.Text);
                    }
                }
            }
            catch (Exception er)
            {

            }
        }
   
            //Console.WriteLine("文件新建事件处理逻辑 {0}  {1}  {2}", e.ChangeType, e.FullPath, e.Name);
        private  void OnChanged(object source, FileSystemEventArgs e)
        {
            try
            {
                string searchstr = "select id from wordfiles where fullpath='" + e.FullPath + "'";
                var ds = SQLiteHelper.ExecuteDataSet(searchstr, CommandType.Text);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string updatesql = "update wordfiles set ";
                    updatesql += "fullpath='" + e.FullPath + "',";
                    var pname = e.Name.Substring(0, e.Name.IndexOf('.'));
                    updatesql += "pname='" + pname + "',";
                    updatesql += "uploadtime=datetime('now','localtime'),";
                    var html = Word2HtmlHandler.Word2Html(e.FullPath);
                    updatesql += "htmlcon='" + html + "',";
                    var text = Word2HtmlHandler.Word2Txt(e.FullPath);
                    updatesql += "txtcon='" + text + "' ";
                    updatesql += " where fullpath='" + e.FullPath + "'";
                    if (html != "" && text != "")
                    {
                        int i = SQLiteHelper.ExecuteNonQuery(updatesql, CommandType.Text);
                    }
                }
                else
                {
                    string sql = "insert into wordfiles(pname,fullpath,uploadtime,state,htmlcon,txtcon)values(";
                    var pname = e.Name.Substring(0, e.Name.IndexOf('.'));
                    sql += "'" + pname + "',";
                    sql += "'" + e.FullPath + "',";
                    sql += "datetime('now','localtime'),";
                    sql += "0,";
                    var html = Word2HtmlHandler.Word2Html(e.FullPath);
                    sql += "'" + html + "',";
                    var text = Word2HtmlHandler.Word2Txt(e.FullPath);
                    sql += "'" + text + "')";
                    if (html != "" && text != "")
                    {
                        int i = SQLiteHelper.ExecuteNonQuery(sql, CommandType.Text);
                    }

                }
            }
            catch (Exception err)
            {

            }




            //Console.WriteLine("文件改变事件处理逻辑{0}  {1}  {2}", e.ChangeType, e.FullPath, e.Name);
        }

        private  void OnDeleted(object source, FileSystemEventArgs e)
        {
            try
            {
                string searchstr = "select id from wordfiles where fullpath='" + e.FullPath + "'";
                var ds = SQLiteHelper.ExecuteDataSet(searchstr, CommandType.Text);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    int i = SQLiteHelper.ExecuteNonQuery("delete from wordfiles where fullpath='" + e.FullPath + "'", CommandType.Text);
                }
            }
            catch (Exception errr)
            {

            }

            //Console.WriteLine("文件删除事件处理逻辑{0}  {1}   {2}", e.ChangeType, e.FullPath, e.Name);
        }

        private  void OnRenamed(object source, RenamedEventArgs e)
        {
            Console.WriteLine("文件重命名事件处理逻辑{0}  {1}  {2}", e.ChangeType, e.FullPath, e.Name);
        }
        /// <summary>
        /// 最小化时隐藏到右下角
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)  //判断是否最小化
            {
                this.ShowInTaskbar = false;  //不显示在系统任务栏
                notifyIcon1.Visible = true;  //托盘图标可见
            }
        }
        /// <summary>
        /// 双击托盘显示主窗体或隐藏主窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.ShowInTaskbar = true;  //显示在系统任务栏
                this.WindowState = FormWindowState.Normal;  //还原窗体
                //notifyIcon.Visible = false;  //托盘图标隐藏
            }
            if (this.WindowState == FormWindowState.Normal || this.WindowState == FormWindowState.Maximized)
            {

                this.Hide();
                this.WindowState = FormWindowState.Minimized;
                this.ShowInTaskbar = false;  //不显示在系统任务栏
                notifyIcon1.Visible = true;  //托盘图标可见
            }
        }
        /// <summary>
        /// 显示主窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void showform_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.ShowInTaskbar = true;
                this.WindowState = FormWindowState.Normal;
                //notifyIcon.Visible = false;
            }
        }
        /// <summary>
        /// 销毁控件
        /// </summary>
        private void DisPanel()
        {
            foreach (UserControl uc in this.panel1.Controls)
            {
                uc.Dispose();
            }

        }

        private void btnDelMeeting_Click(object sender, EventArgs e)
        {
            var uc = new UCImport();
            uc.Dock = DockStyle.Fill;
            DisPanel();
            this.panel1.Controls.Add(uc);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确实要退出系统吗?", "退出系统", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                //Application.Exit();
                System.Environment.Exit(0);
            }
        }

        private void btnWeb_Click(object sender, EventArgs e)
        {
            //WEB服务器
            var web = new UCWeb();
            web.Dock = DockStyle.Fill;
            DisPanel();
            this.panel1.Controls.Add(web); 
        }
    }
}
